/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package qlvbdq.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import qlvbdq.dconnection.SQLServerConnect;
import qlvbdq.dto.CTPhieuNhapHangDTO;

/**
 *
 * @author tuan
 */
public class CTPhieuNhapHangDAO {
    
    public CTPhieuNhapHangDAO() {
    }
    
    public ArrayList<CTPhieuNhapHangDTO> GetCTPhieuNhapHangByMaPMH(String MaPMH) throws SQLException {
        ArrayList<CTPhieuNhapHangDTO> listCT = new ArrayList<>();
        Object[] objs = { MaPMH };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetCTPhieuNHByMaPhieuMHFromDatabase( ? )", objs);
        CTPhieuNhapHangDTO ctPhieuMH;
        while (result.next()) {
            ctPhieuMH = new CTPhieuNhapHangDTO(result.getString("MAPHIEUNHAP"), result.getString("MASP"), 
                    result.getInt("SOLUONG"),result.getLong("DONGIANHAP"), result.getLong("THANHTIEN"));
            listCT.add(ctPhieuMH);
        }
        return listCT;
    }
    
    public ArrayList<CTPhieuNhapHangDTO> GetCTPhieuNhapHangByMaSanPham(String MaSP) throws SQLException {
        ArrayList<CTPhieuNhapHangDTO> listCT = new ArrayList<>();
        Object[] objs = { MaSP };
        ResultSet result = SQLServerConnect.getInstance().getDataFromStore("GetCTPhieuNHByMaSPFromDatabase( ? )", objs);
        CTPhieuNhapHangDTO ctPhieuMH;
        while (result.next()) {
            ctPhieuMH = new CTPhieuNhapHangDTO(result.getString("MAPHIEUMH"), result.getString("MASP"), 
                    result.getInt("SOLUONG"),result.getLong("DONGIANHAP"), result.getLong("THANHTIEN"));
            listCT.add(ctPhieuMH);
        }
        return listCT;
    }
    
    public int UpdateCTPhieuNH(CTPhieuNhapHangDTO ctphieuNH) {
        Object[] objs = { ctphieuNH.getMaPhieuNhap(), ctphieuNH.getMaSP(), ctphieuNH.getSoLuong(), 
            ctphieuNH.getDonGiaNhap(), ctphieuNH.getThanhTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("UpdateCTPhieuNhapHangToDatabase(?, ? ,?, ?)", objs);
    }
    
    public int DeleteCTPhieuNH(CTPhieuNhapHangDTO ctPhieuMH) {
        Object[] objs = { ctPhieuMH.getMaPhieuNhap(), ctPhieuMH.getMaSP() };
        return SQLServerConnect.getInstance().updateDataToDatabase("DeleteChiTietPNHToDatabase(?, ?)", objs);
    }
    
    public int InsertCTPhieuNH(CTPhieuNhapHangDTO ctPhieuMH) {
        Object[] objs = { ctPhieuMH.getMaPhieuNhap(), ctPhieuMH.getMaSP(), ctPhieuMH.getSoLuong(), 
                    ctPhieuMH.getDonGiaNhap(), ctPhieuMH.getThanhTien()};
        return SQLServerConnect.getInstance().updateDataToDatabase("InsertChiTietPNHToDatabase(?, ? ,?, ?, ?)", objs);
    }
}
